package com.pet.controller;

import com.pet.pojo.PageResult;
import com.pet.pojo.Pet;
import com.pet.pojo.PetQueryParam;
import com.pet.pojo.Result;
import com.pet.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 宠物控制器
 */
@RestController
@RequestMapping("/api/pets")
public class PetController {
    
    @Autowired
    private PetService petService;
    
    /**
     * 分页查询宠物列表
     * @param param 查询条件
     * @return 分页结果
     */
    @PostMapping("/list")
    public Result<PageResult<Pet>> list(@RequestBody PetQueryParam param) {
        PageResult<Pet> pageResult = petService.queryPetList(param);
        return Result.success(pageResult);
    }
    
    /**
     * 根据ID查询宠物
     * @param id 宠物ID
     * @return 宠物信息
     */
    @GetMapping("/{id}")
    public Result<Pet> getById(@PathVariable Integer id) {
        Pet pet = petService.getPetById(id);
        if (pet == null) {
            return Result.error("宠物不存在");
        }
        return Result.success(pet);
    }
    
    /**
     * 新增宠物
     * @param pet 宠物信息
     * @return 操作结果
     */
    @PostMapping
    public Result<Void> add(@RequestBody Pet pet) {
        boolean success = petService.addPet(pet);
        if (success) {
            return Result.success("添加成功", null);
        } else {
            return Result.error("添加失败");
        }
    }
    
    /**
     * 更新宠物信息
     * @param id 宠物ID
     * @param pet 宠物信息
     * @return 操作结果
     */
    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Integer id, @RequestBody Pet pet) {
        pet.setId(id);
        boolean success = petService.updatePet(pet);
        if (success) {
            return Result.success("更新成功", null);
        } else {
            return Result.error("更新失败");
        }
    }
    
    /**
     * 删除宠物
     * @param id 宠物ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Integer id) {
        boolean success = petService.deletePet(id);
        if (success) {
            return Result.success("删除成功", null);
        } else {
            return Result.error("删除失败");
        }
    }
} 